﻿using HMACAuthentication.WebApi.Filters;
using YMJF_Service.Common;
using YMJF_Service.Helpers;
using YMJF_Service.Models;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace YMJF_Service.Controllers
{
    /// <summary>
    /// 附件上传管理
    /// </summary>
    [RoutePrefix("api/accessory")]
    public class AccessoryController : ApiController
    {
        ConnectionStringMySQLDB db = new ConnectionStringMySQLDB();

        /// <summary>
        /// 获取附件列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="pagesize">每页行数</param>  
        /// <param name="key">名称</param>
        /// <param name="FID">关联ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        public ApiResult GetList(long page, long pagesize, string key, string FID)
        {
            ApiResult res = new ApiResult();
            try
            {
                string sqlwhere = "";
                if (!string.IsNullOrEmpty(key))
                {
                    sqlwhere += string.Format(" and Name like '%{0}%' ", key);
                }
                if (!string.IsNullOrEmpty(FID))
                {
                    sqlwhere += string.Format(" and FID ='{0}' ", FID);
                }
                string sql = string.Format(@"select a.*,d1.ItemName as CategoryName from Accessory a 
                                            left join DictionaryDetail d1 on d1.DictCode ='AccessoryCategory' and d1.ItemCode =a.Category
                                            where a.SysDeleted=0 {0} order by a.SysOrder,a.SysLastUpd desc", sqlwhere);
                Page<object> list = db.Page<object>(page, pagesize, sql);
                res.code = HttpCode.success;
                res.message = "获取列表成功";
                res.data = list;
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("accessory", "获取附件列表错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }


        /// <summary>
        /// 获取附件信息
        /// </summary>
        /// <param name="ID">附件ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("get")]
        public ApiResult GetAccessory(string ID)
        {
            ApiResult res = new ApiResult();
            try
            {
                accessory data = accessory.SingleOrDefault(" where SysDeleted = 0 and ID = @0 ", ID);
                if (data != null )
                {
                    res.code = HttpCode.success;
                    res.message = "查询成功";
                    res.data = data;
                }
                else
                {
                    res.code = HttpCode.Que_Failed;
                    res.message = "查询失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("accessory", "获取附件信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }



        /// <summary>
        /// 添加附件信息
        /// </summary>
        /// <param name="data">附件信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("add")]
        public ApiResult AddAccessory(accessory data)
        {
            ApiResult res = new ApiResult();

            try
            {
                string user = "admin";
                data.ID = Guid.NewGuid().ToString();
                data.SysCreate = DateTime.Now;
                data.SysCreatedBy = user;
                data.SysLastUpd = DateTime.Now;
                data.SysLastUpdBy = user;
                data.SysDeleted = false;
                object obj = data.Insert();
                if (obj != null)
                {
                    res.code = HttpCode.success;
                    res.message = "添加成功";
                    res.data = data;
                }
                else
                {
                    res.code = HttpCode.Add_Failed;
                    res.message = "添加失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("accessory", "添加附件信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }

        /// <summary>
        /// 修改附件信息
        /// </summary>
        /// <param name="data">附件信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("update")]
        public ApiResult UpdateAccessory(accessory data)
        {
            ApiResult res = new ApiResult();

            try
            {
                accessory data2 = accessory.SingleOrDefault(" where ID = @0 ", data.ID);
                data2.FID = data.FID;
                data2.Name = data.Name;
                data2.Type = data.Type;
                data2.Url = data.Url;
                data2.remark = data.remark;
                data2.SysOrder = data.SysOrder;
                data2.SysLastUpd = DateTime.Now;
                int obj = data2.Update();
                if (obj > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "修改成功";
                }
                else
                {
                    res.code = HttpCode.Mod_Failed;
                    res.message = "修改失败";
                }

            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("accessory", "修改附件信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }


        /// <summary>
        ///  删除附件信息
        /// </summary>
        /// <param name="ID">附件ID</param>
        /// <returns></returns>
        [HttpPost]
        [Route("del")]
        public ApiResult DelAccessory(string ID)
        {
            ApiResult res = new ApiResult();

            try
            {
                int obj = accessory.Update(string.Format(" set SysDeleted = 1,SysLastUpd = '{1}' where ID = '{0}' ", ID, DateTime.Now));
                if (obj > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "删除成功";
                }
                else
                {
                    res.code = HttpCode.Del_Failed;
                    res.message = "删除失败";
                }

            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("accessory", "删除附件信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }


    }
}
